package com.huonan.jb.tools.other;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

import java.awt.*;
import java.util.Date;

/**
 * @author lxp,模拟鼠标移动
 */
@Slf4j
public final class JavaRobotExample {

    public static final int DELAY_MS = 1000 * 10;
    public static final String SET_TIME = "2024-07-11 18:20:10";

    public static void main(String[] args) throws AWTException {

        while (true) {
            Date terminalDate = getTermSecond();
            Date nowDate = getNowSecond();
            int compare = DateUtil.compare(nowDate, terminalDate, "yyyy-MM-dd HH:mm:ss");
            if (compare > 0) {
                log.info("结束");
                System.exit(0);
            }
            long timeRemaining = DateUtil.betweenMs(nowDate, terminalDate);
            String s = DateUtil.secondToTime((int) timeRemaining / 1000);
            log.info("结束时间剩余: {}", s);
            handleMouse();
        }
    }

    private static Date getNowSecond() {
        String now = DateUtil.now();
        Date nowDate = DateUtil.parse(now);
        log.info("当前时间: {}", nowDate);
        return nowDate;
    }

    private static Date getTermSecond() {
        Date termDate = DateUtil.parse(SET_TIME);
        log.info("设定时间: {}", termDate);
        return termDate;
    }

    private static void handleMouse() throws AWTException {
        Robot robot = new Robot();
        robot.setAutoDelay(5);
        robot.setAutoWaitForIdle(true);
        robot.mouseMove(1000, 100);
        robot.delay(DELAY_MS);
        robot.mouseMove(1000, 1000);
        // robot.delay(DELAY_MS);
        // robot.mouseMove(200, 0);
    }
}